﻿using HandyControl.Controls;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using WpfAGVSystem.Common;
using WpfAGVSystem.DBModel;
using WpfAGVSystem.InterFace;

namespace WpfAGVSystem.ViewModels
{
    public class LoginViewModel : BindableBase , IDialogAware
    {
        public readonly IEventAggregator eventAggregator;
        private IDBInterface dB;
        public void WaitFor(bool Isopen)
        {
            eventAggregator.GetEvent<LoginEvent>().Publish(Isopen);
        }
        public LoginViewModel(IEventAggregator _eventAggregator, IDBInterface dBInterface)
        {
            eventAggregator= _eventAggregator;  
            LoginCommand = new DelegateCommand<string>(Excute);
            dB = dBInterface;
           // Pwd = "123";


        }
        public DelegateCommand<string> LoginCommand { get; private set; }
        public string Pwd { get; set; }
        public string UserName { get; set; }
        public string Title => "登录";

        public event Action<IDialogResult> RequestClose;

        public void Excute(string arg)
        {
   
            switch (arg)
            {
                case "login":
                    Task.Run(Login);
                    break;
                case "loginout":
                    break;
            }
        }
        public async void Login()
        {       
            if (string.IsNullOrEmpty(UserName))
            {             
                Growl.Info("请输入用户名");
                return;
            }
            if (string.IsNullOrEmpty(Pwd))
            {
                Growl.Info("请输入密码");
                return;
            }
            WaitFor(true);
            //  await Task.Delay(5000);
            var user = dB.FirstOrDefault<User_Entity>(x => x.userName == UserName && x.passWord == Pwd);
            Role_Entity role=null;
            if (user!=null)
            {
                role =  dB.FirstOrDefault<Role_Entity>(x => x.rol1ID == user.rollID);
            }
            if ((UserName == "Admin" && Pwd == "Admin" ) || (user!=null && role!=null))
            {
                UserForm_EF userForm_EF  = new UserForm_EF();
                if (UserName == "Admin" && Pwd == "Admin")
                {
                    userForm_EF.user = new User_Entity() { userName = "Admin"  };
                    userForm_EF.Role = new Role_Entity() { rol1Name="超级管理员" };
                    userForm_EF.Role.AllTrue();
                }
                else 
                {
                    userForm_EF.user = user;
                    userForm_EF.Role = role;
                }
                App.Current.Dispatcher.Invoke(callback: () =>
                {
                    eventAggregator.GetEvent<UserEvent>().Publish(userForm_EF);
                    RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                    WaitFor(false);
                });
            }
            else 
            {
                WaitFor(false);
                Growl.Info("用户名或密码错误");
            }
            //if (string.IsNullOrEmpty(UserName))
            //{
            //    WaitFor(false);
            //    eventAggregator.GetEvent<InfoMessgeEvevnt>().Publish("请输入用户名");
            //    return;
            //}
            //if (string.IsNullOrEmpty(PassWorld))
            //{
            //    WaitFor(false);
            //    eventAggregator.GetEvent<InfoMessgeEvevnt>().Publish("请输入密码");
            //    return;
            //}
            //UserForm_EF User = null;
            //using (EFModel eF = new EFModel())
            //{
            //    User = eF.UserFrom_EFs.ToList()[0];
            //}

            //if (User != null)
            //{
            //    WaitFor(false);
            //    App.Current.Dispatcher.Invoke(callback: () =>
            //    {
            //        eventAggregator.GetEvent<UserInfoEvevnt>().Publish(User);
            //        RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            //    });
            //}
            //else
            //{
            //    eventAggregator.GetEvent<InfoMessgeEvevnt>().Publish("用户名与密码错误");
            //}
          //  Task.Delay(5000).Wait();
           // WaitFor(false);
        }
        public bool CanCloseDialog()
        {
            return true;
            //throw new NotImplementedException();
        }

        public void OnDialogClosed()
        {
           // throw new NotImplementedException();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
           // throw new NotImplementedException();
        }
    }
}
